//
//  YMStatusPictureView.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/25.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit

let pictureCellID = "collectioncellID"
//MARK: -配图的view
class YMStatusPictureView: UICollectionView {

    //cell的间距
    let cellMargin: CGFloat = 5
    //cell的宽高
    let cellWH: CGFloat = 80
    
    var picuserInfo:[YMPictureInfo]?{
        didSet{
            //
            countLabel.text = "\(picuserInfo?.count ?? 0)"
        self.snp_updateConstraints { (make) in
            make.size.equalTo(getPictureViewSize(picuserInfo?.count ?? 0))
            
        }
        reloadData()
        }
    }

    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())

        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        backgroundColor = UIColor.whiteColor()
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSizeMake(80, 80)
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 5
        
        registerClass(YMStatusPictureCell.self, forCellWithReuseIdentifier:pictureCellID )
        
        dataSource = self
        addSubview(countLabel)
        
        // 添加约束
        countLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
    }
    
    //计算视图的size
    func getPictureViewSize(count:Int) ->CGSize{
        let col = count == 4 ? 2 : count >= 3 ? 3 : count
        let row = count == 4 ? 2 : (count - 1)/3 + 1
//        let col = count == 4 ? 2 : count >= 3 ? 3 : count
//        let row = count == 4 ? 2 : (count - 1)/3 + 1
        let w = CGFloat(col)*80 + CGFloat(col-1)*5
        let h = CGFloat(row)*80 + CGFloat(row-1)*5
        
        
    return CGSize(width: w, height: h)
    
    }
    
    
    
    
    
    
    
    
    
    
    //        MARK: - 懒加载控件
    
    lazy var countLabel:UILabel = {
       let lab = UILabel()
        
        lab.textColor = UIColor.redColor()
        lab.font = UIFont.systemFontOfSize(35)
        return lab
        
    }()

}


extension YMStatusPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picuserInfo?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellID, forIndexPath: indexPath) as! YMStatusPictureCell
        // cell赋值
//        cell.backgroundColor = RandomColor()
        cell.pic_url = picuserInfo![indexPath.row]

        return cell
    }
}


class YMStatusPictureCell: UICollectionViewCell {
    
    //提供一个供外界赋值的接口
    
    var pic_url:YMPictureInfo?{
        
        didSet{
            bgImageView.sh_setImage(pic_url?.thumbnail_pic, placeholderImgName: "weibo_placeholder")
            
            //判断是否为gif
            if let thPic = pic_url?.thumbnail_pic where thPic.hasSuffix(".gif"){
                
                gifImageView.hidden = false
            }else{
                gifImageView.hidden = true
            }
            
        }
    }
    
    
    
    
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        backgroundColor = RandomColor()
        //Tianjia
        contentView.addSubview(bgImageView)
        contentView.addSubview(gifImageView)
//        /约束
        bgImageView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImageView.snp_makeConstraints { (make) in
           make.bottom.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    //        MARK: - 懒加载
    //背景图片
    lazy var bgImageView:UIImageView = {
       let bg = UIImageView()
        bg.contentMode = .ScaleAspectFill
        bg.layer.masksToBounds = true
        return bg
        
    }()
    //动态logo
    lazy var gifImageView:UIImageView = UIImageView(imgName: "timeline_image_gif")
}












